package com.hockeo.client.ui.widgets;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hockeo.client.VHMain;
import com.hockeo.client.model.JSData;
import com.hockeo.client.res.AppConstants;
import com.hockeo.client.ui.base.VHEditorGrid;
import com.hockeo.client.ui.base.VHGridContextMenu;
import com.hockeo.client.ui.menu.GridDataEditorContextMenu;
import com.hockeo.shared.rpc.classic.RpcReceiverCallback;

/**
 * Grid for editing associations.
 *
 * @version $Id$
 * @author jjanke
 */
public class GridDataEditorAssociation extends VHEditorGrid
{
  @Override
  protected VHGridContextMenu createContextMenu()
  {
    return new GridDataEditorContextMenu( this );
  }

  @Override
  protected ColumnModel createColumnModel()
  {
    final AppConstants c = VHMain.getAppConstants();
    final ArrayList<ColumnConfig> listCols = new ArrayList<ColumnConfig>();
    ColumnConfig col;

    listCols.add( getRowNumberer() );

    // id
    //
    col = new ColumnConfig();
    col.setId( JSData.PARAM_ID );
    col.setHeader( c.associationIdAbbrev() );
    col.setToolTip( c.associationId() );
    col.setWidth( 50 );
    col.setAlignment( HorizontalAlignment.LEFT );
    col.setResizable( true );
    col.setEditor( new CellEditor( new TextField<String>() ) );
    listCols.add( col );

    // associated countries
    //
    col = new ColumnConfig();
    col.setId( "countries" );
    col.setHeader( c.associationAssociatedCountriesAbbrev() );
    col.setToolTip( c.associationAssociatedCountries() );
    col.setWidth( 200 );
    col.setAlignment( HorizontalAlignment.LEFT );
    col.setResizable( true );
    col.setEditor( new CellEditor( new TextField<String>() ) );
    listCols.add( col );
    setAutoExpandColumn( col );

    return new ColumnModel( listCols );
  }

  @Override
  protected String getEditedDataType()
  {
    return "com.hockeo.server.data.entity.Association";
  }

  @Override
  public void loadData( Object... aobjConfig )
  {
    AsyncCallback<List<JSData>> cb = new RpcReceiverCallback<List<JSData>>( this, null );
    VHMain.getRpcClassicService().getAllAssociations( cb );
  }
}
